package com.sinothk.gpserver.connection.websocket;

import com.sinothk.gpserver.connection.websocket.bean.Player;
import com.sinothk.gpserver.connection.websocket.bean.RoomIM;
import com.sinothk.gpserver.data.WsConstant;
import com.sinothk.gpserver.modules.texasPoker.helper.Poker;
import com.sinothk.gpserver.modules.texasPoker.helper.TexasPokerBean;
import com.sinothk.gpserver.modules.texasPoker.helper.TexasPokerHelper;

import java.util.ArrayList;

/**
 * 编写 Liang Y.T.
 * 时间 2021/7/20 10:25
 */
public class TexasIm {

    public static void sendPersonCard(RoomIM roomIM) {
        // 获得洗后的牌
        Poker[] pokers = Poker.getEnablePokerFor7s(roomIM.getRoomType());


        // 根据参与人数发牌

        ArrayList<Player> playerList = new ArrayList<>();
        for (Player player : roomIM.getPlayers().values()) {
            if (player.getRunState() == WsConstant.PLAYER_RUN.player) {
                playerList.add(player);
            }
        }
        TexasPokerBean texasPokerInfo = TexasPokerHelper.sendPersonCard(pokers, playerList.size());

        // 给每个玩家分配牌
        for (int i = 0; i < playerList.size(); i++) {
            ArrayList<Poker> userPokers = texasPokerInfo.getUserPokerList().get(i);

            ArrayList<String> userStrPokers = new ArrayList<>();
            for (Poker poker : userPokers) {
                userStrPokers.add(poker.toString());
            }

            playerList.get(i).setUserPokers(userStrPokers);
        }

        for (Player player : playerList) {
            roomIM.getPlayers().put(player.getSeatNum(), player);
        }

        // 设置剩下牌
        roomIM.setTexasPoker(texasPokerInfo);
    }

    public static void sendPubCard(RoomIM roomIM) {
        // 发公共牌
        roomIM.setTexasPoker(TexasPokerHelper.sendPubCard(roomIM.getTexasPoker()));
        roomIM.setHasSendPubCard(true);
    }

    public static void sendOneCard(RoomIM roomIM) {
        // 只发一张
        roomIM.setTexasPoker(TexasPokerHelper.sendOneCard(roomIM.getTexasPoker()));
    }

    public static void sendPubRemainingCards(RoomIM roomIM) {
        // 一次发完剩余公共牌
        roomIM.setTexasPoker(TexasPokerHelper.sendPubRemainingCards(roomIM.getTexasPoker()));
        roomIM.setHasSendPubCard(true);
    }

    public static void sendOneCardToPrePlayer(RoomIM roomIM) {
        // 只发一张给每个玩家
        for (Player player : roomIM.getPlayers().values()) {
            if (player.getRunState() != WsConstant.PLAYER_RUN.preparer && // 不是准备人
                    player.getRunState() != WsConstant.PLAYER_RUN.empty && // 不是空用户
                    player.getOptState() != WsConstant.PLAYER_OPT.STATE_CANCEL // 不是弃牌人
            ) {
                TexasPokerBean texasPoker = roomIM.getTexasPoker();

                String addCard = TexasPokerHelper.sendOneCardToPrePlayer(texasPoker);
                ArrayList<String> userPokers = player.getUserPokers();
                userPokers.add(addCard);
                player.setUserPokers(userPokers);
            }
        }
    }

//    @Deprecated
//    public static RoomIM initPlaying(RoomIM roomIM) {
//        // 获得洗后的牌
//        Poker[] pokers = Poker.getEnablePoker();
//        // 根据参与人数发牌
//        ConcurrentHashMap<String, Player> players = roomIM.getPlayers();
//        int playerSize = players.size();
//        // 给每个人发牌
//        TexasPokerBean texasPokerBean = TexasPokerHelper.getStartPoker(pokers, playerSize);
//        // 设置房间牌信息
//        roomIM.setTexasPoker(texasPokerBean);
//        // 设置房间底牌
//        ArrayList<String> basePokers = new ArrayList<>();
//        for (Poker poker : texasPokerBean.getPublicPokerList()) {
//            basePokers.add(poker.toString());
//        }
//        roomIM.setPubPokers(basePokers);
//
//        int i = 0;
//        for (String key : roomIM.getPlayers().keySet()) {
//            ArrayList<Poker> userPokers = texasPokerBean.getUserPokerList().get(i);
//
//            ArrayList<String> userStrPokers = new ArrayList<>();
//            for (Poker poker : userPokers) {
//                userStrPokers.add(poker.toString());
//            }
//            roomIM.getPlayers().get(key).setUserPokers(userStrPokers);
//
//            i++;
//        }
////
////        // 设置游戏为进行中
////        roomIM.setBaseCoins(0);
////        roomIM.setStats(RoomIM.STATE_ROOM_PLAYING);
//        return roomIM;
//    }


}
